from app.extensions import db
from datetime import datetime

class DeviceApiConfig(db.Model):
    __tablename__ = 'device_api_configs'

    id = db.Column(db.Integer, primary_key=True)
    venue_id = db.Column(db.Integer, db.ForeignKey('venues.id'), nullable=False)
    name = db.Column(db.String(128), nullable=False)
    description = db.Column(db.String(255), nullable=True)
    device_type = db.Column(db.String(50), nullable=False)
    api_endpoint = db.Column(db.String(512), nullable=False)
    http_method = db.Column(db.String(10), nullable=False, default='POST')
    headers_template = db.Column(db.JSON, nullable=True)
    body_template = db.Column(db.JSON, nullable=True)
    status = db.Column(db.String(20), nullable=False, default='active')  # active, inactive
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)

    venue = db.relationship('Venue', backref=db.backref('device_api_configs', lazy=True))

    def to_dict(self, full=True):
        data = {
            'id': self.id,
            'venue_id': self.venue_id,
            'name': self.name,
            'description': self.description,
            'device_type': self.device_type,
            'status': self.status,
        }
        if full:
            data.update({
                'api_endpoint': self.api_endpoint,
                'http_method': self.http_method,
                'headers_template': self.headers_template,
                'body_template': self.body_template,
                'created_at': self.created_at.isoformat(),
                'updated_at': self.updated_at.isoformat()
            })
        return data 